Robotics 1.0 Lesson Plan
Lesson 8 - Serve It Cool

Purpose: Enable students to connect a servo motor to the CPX and control its movement based on sensors such as light or temperature.

No. of Classes

1 class (Duration: 1 hour 30 minutes; maximum 4 students per group).

Materials Required

    Desktop or laptop computer, CPX board, USB cable

    Micro Servo 9g

Prior Knowledge

Adafruit Circuit Playground Express (CPX) board

USB cable

Computer with Mu Editor

Exercises

Exercise:1



Servo Control Using CPX Toggle Switch


Click here to view the connection diagram for a servo motor and CPX
Click here to view the instructions and code

Exercise:2



Using Try-Except block in CircuitPython


Click here to view the instructions and code

Exercise:3



Handling Servo Motor Errors Using Try-Except in CircuitPython


Sample code:

Click here to view the instructions and code (without try-except)


How to fix the error:

Click here to view the instructions and code (with try-except)

Solution



Here is the solution:

Click here to see the solution video for Exercise 1
Click here to see the solution video for Exercise 3 (without try-except)
Click here to see the solution video for Exercise 3 (with try-except)
Teacher's Instruction
  1. Explain about servo motors and PWM (Pulse Width Modulation) - explanation and diagram
  2. Introduce the required materials (CPX, USB cable, Mu Editor, servo motor – 180°).
  3. Simple Explanation:
    • Introduce students to different types of servos - 90, 180 and continuous servo motors and highlight how they are different
    • Teach students that a 180 servo motor moves only between 0° and 180°.
    • Show how to control the servo using CircuitPython and the servo.angle command.
    • Explain that setting angles in a 180 server outside the range, like 360° will cause an error.
    • Use a try–except block to safely handle invalid angles.
    • Students run the code and observe servo movements and REPL (Read, Evaluate, Print, Loop) messages.
    • Provide step-by-step instructions and demonstrate troubleshooting techniques.
  4. View the code here
    What is the value of final_state in this? Talk about the motor_on=False statement inside the try-except block, why it is not reachable and different ways to ensure it executes
  5. Is it better to prevent errors or handle them when they happen?